//
//  HWEssenceTool.m
//  JWC
//
//  Created by lanou on 16/4/11.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWEssenceTool.h"
#import "MJExtension.h"
#import "HWRecommendModel.h"
#import "HWCommentModel.h"
#import "HWDetailModel.h"

@implementation HWEssenceTool

// 下拉刷新
- (void)getDataWithType:(TopicType)type style:(NSString *)style block:(void (^) (id json, NSInteger param))block
{

    NSString *url = [NSString stringWithFormat:@"http://s.budejie.com/topic/list/%@/%ld/bs0315-iphone-4.1/0-20.json", style, type];
    [HttpTool post:url parameters:nil success:^(id json) {
        NSArray *listArray = [HWRecommendModel mj_objectArrayWithKeyValuesArray:json[@"list"]];
        NSString *np = (NSString *)json[@"info"][@"np"];
        NSInteger num = [np integerValue];
        block(listArray,num);
    } failure:^(NSError *error) {
        NSLog(@"网络请求失败");
    }];
}

// 上拉加载
- (void)getDataWithNp:(NSInteger)np type:(TopicType)type style:(NSString *)style block:(void (^) (id json, NSInteger param))block
{
    NSString *url = [NSString stringWithFormat:@"http://s.budejie.com/topic/list/%@/%ld/bs0315-iphone-4.1/%ld-20.json", style, type, np];
    [HttpTool post:url parameters:nil success:^(id json) {
        NSArray *listArray = [HWRecommendModel mj_objectArrayWithKeyValuesArray:json[@"list"]];
        NSString *np = (NSString *)json[@"info"][@"np"];
        NSInteger num = [np integerValue];
        block(listArray,num);
    } failure:^(NSError *error) {
        NSLog(@"网络请求失败");
    }];
}

// 下拉刷新评论
- (void)getCommentsWithID:(NSString *)ID block:(void (^) (id json, NSString *param))block
{
    // 参数
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"a"] = @"dataList";
    dic[@"c"] = @"comment";
    dic[@"data_id"] = ID;
    [HttpTool post:@"http://api.budejie.com/api/api_open.php?" parameters:dic success:^(id json) {
        NSArray *commentArray = [HWCommentModel mj_objectArrayWithKeyValuesArray:json[@"data"]];
        HWCommentModel *model = commentArray.lastObject;
        NSString *param = model.id;
        block(commentArray, param);
        
    } failure:^(NSError *error) {
        NSLog(@"网络请求错误");
    }];
}

// 上拉加载更多评论
- (void)getCommentsWithID:(NSString *)ID page:(NSInteger)page lastcid:(NSString *)lastcid block:(void (^)(id json, NSString *total))block
{
    // 参数
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"a"] = @"dataList";
    dic[@"c"] = @"comment";
    dic[@"data_id"] = ID;
    dic[@"page"] = [NSString stringWithFormat:@"%ld", page];
    dic[@"lastcid"] = lastcid;
    
    [HttpTool post:@"http://api.budejie.com/api/api_open.php?" parameters:dic success:^(id json) {
        if ([json isKindOfClass:[NSDictionary class]])
        {
            NSArray *moreCommentArray = [HWCommentModel mj_objectArrayWithKeyValuesArray:json[@"data"]];
            //        NSInteger total = [json[@"total"] integerValue];
            HWCommentModel *model = moreCommentArray.lastObject;
            NSString *param = model.id;
            
            block(moreCommentArray,param);
        }
        else
        {
            return ;
        }
        
    } failure:^(NSError *error) {
        NSLog(@"网络请求错误");
    }];
}

// 加载表头
- (void)getHeadWithID:(NSString *)ID block:(void (^) (id json))block
{
    // 参数
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"a"] = @"profile";
    dic[@"c"] = @"user";
    dic[@"userid"] = ID;
    
    [HttpTool post:@"http://api.budejie.com/api/api_open.php?" parameters:dic success:^(id json) {
        NSDictionary *dataDic = json[@"data"];
        HWDetailModel *model = [[HWDetailModel alloc]init];
        [model setValuesForKeysWithDictionary:dataDic];
        
        block(model);
    } failure:^(NSError *error) {
        NSLog(@"网络请求错误");
    }];
}

// 刷新
- (void)getDetailDataWithID:(NSString *)ID block:(void (^) (id json, NSInteger np))block
{
    [HttpTool post:[NSString stringWithFormat:@"http://s.budejie.com/topic/user-topic/%@/1/desc/bs0315-iphone-4.1/0-20.json", ID] parameters:nil success:^(id json) {
        NSArray *listArray = [HWRecommendModel mj_objectArrayWithKeyValuesArray:json[@"list"]];
        NSString *np = (NSString *)json[@"info"][@"np"];
        NSInteger num = [np integerValue];
        block(listArray,num);
    } failure:^(NSError *error) {
        NSLog(@"网络请求错误");
    }];
}

// 上拉加载
- (void)getDetailDataWithID:(NSString *)ID np:(NSInteger)np block:(void (^) (id json, NSInteger np))block
{
    [HttpTool post:[NSString stringWithFormat:@"http://s.budejie.com/topic/user-topic/%@/1/desc/bs0315-iphone-4.1/%ld-20.json", ID, np] parameters:nil success:^(id json) {
        NSArray *listArray = [HWRecommendModel mj_objectArrayWithKeyValuesArray:json[@"list"]];
        NSString *np = (NSString *)json[@"info"][@"np"];
        NSInteger num = [np integerValue];
        block(listArray,num);
    } failure:^(NSError *error) {
        NSLog(@"网络请求错误");
    }];
}

// 刷新
- (void)getTakeDetailDataWithID:(NSInteger)ID type:(NSString *)type block:(void (^) (id json, NSInteger np))block
{
    NSString *url = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/%@/bs0315-iphone-4.1/0-20.json", ID, type];
    [HttpTool post:url parameters:nil success:^(id json) {
        NSArray *listArray = [HWRecommendModel mj_objectArrayWithKeyValuesArray:json[@"list"]];
        NSString *np = json[@"info"][@"np"];
        if ([np isKindOfClass:[NSNull class]])
        {
            return;
        }
        
        NSInteger num = [np integerValue];
        block(listArray,num);
    } failure:^(NSError *error) {
        NSLog(@"网络请求失败");
    }];
}

// 上拉加载
- (void)getTakeDetailDataWithID:(NSInteger)ID type:(NSString *)type np:(NSInteger)np block:(void (^)(id, NSInteger))block
{
    NSString *url = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/%@/bs0315-iphone-4.1/%ld-20.json", ID, type, np];
    [HttpTool post:url parameters:nil success:^(id json) {
        NSArray *listArray = [HWRecommendModel mj_objectArrayWithKeyValuesArray:json[@"list"]];
        NSString *np = json[@"info"][@"np"];
        NSInteger num = [np integerValue];
        block(listArray,num);
    } failure:^(NSError *error) {
        NSLog(@"网络请求错误");
    }];
}


@end

























